package org.lingshi.shopping_manager_api.controller;

import org.apache.dubbo.config.annotation.DubboReference;
import org.lingshi.common.exception.BusException;
import org.lingshi.common.result.BaseResult;
import org.lingshi.common.service.IFileService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Objects;

import static org.lingshi.common.exception.BusCodeEnum.FILE_UPLOAD_ERROR;


@RestController
@RequestMapping("/file")
public class FileController {

    @DubboReference
    private IFileService fileService;

    @PostMapping("/uploadImage")
    public BaseResult uploadImage(MultipartFile file) throws IOException {
        if (Objects.isNull(file)){
            BusException.busException(FILE_UPLOAD_ERROR);
        }
        String filePath = fileService.uploadImage(file.getBytes(), file.getOriginalFilename());
        return BaseResult.success(filePath);
    }
}